﻿using System;
using System.Collections.Generic;

using Caliburn.Micro;

using Client.Specs.Builders;
using Client.Specs.Support;

using FluentAssertions;

using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;

using TheKitchen.Client.Querying;
using TheKitchen.Client.Services;
using TheKitchen.Client.ViewModels;
using TheKitchen.Common;

namespace Client.Specs
{
    public class FindRecipesCoroutineSpecs
    {
        [TestClass]
        [Tag("SearchViewModel")]
        public class When_executing_with_search_criteria : Specification
        {
            private IQueryAgent queryAgent;
            private FindRecipesCoroutine coroutine;
            private Recipe recipe;

            protected override void EstablishContext()
            {
                queryAgent = MockRepository.GenerateStub<IQueryAgent>();

                recipe = new RecipeBuilder().Build();

                queryAgent
                    .Stub(x => x.FindRecipes(null, null, null, null))
                    .IgnoreArguments()
                    .Invoke(Argument.Last, recipe.ToEnumerable());

                coroutine = new FindRecipesCoroutine()
                {
                    QueryAgent = queryAgent,
                    PartialName = "macaroni",
                    PartialDescription = "yummi",
                    MinimumRating = 2
                };
            }

            protected override void Because()
            {
                coroutine.Execute(new ActionExecutionContext());
            }

            [TestMethod]
            public void It_should_call_the_service_agent_to_get_the_recipes_asynchronously()
            {
                queryAgent.AssertWasCalled(q => q.FindRecipes(Arg.Is(2), Arg.Is("macaroni"), Arg.Is("yummi"),
                    Arg<Action<IEnumerable<Recipe>>>.Is.Anything));
            }

            [TestMethod]
            public void It_should_expose_the_recipes_provided_by_the_callback_invocation()
            {
                coroutine.Recipes.Should().Equal(recipe);
            }
        }
    }
}
